<?
  define('PATH',dirname(__FILE__));
  require PATH.'/Modules.php';
  spl_autoload_register(array('Somm','Autoload'));

    
  class Somm {
   
   private static $_project;
   private static $_main;
   const CLASS_EXT          = '.php';
   const PROJECT_PATH       = '../Projects/';
   const CONFIG_PATH        = '/Configs/';
   const TEMPLATE_PATH      = '/Templates/';
   const MODUL_PATH         = '/Classes/';
   private static $_modules     = array();
   private static $_namespaces  = array();
  

   public static function Version() {
    return '0.0021a';
   } 
  
   public static function initErrorHandlers()
   {
    set_error_handler(array('Somm','phpErrorHandler'),error_reporting());
    set_exception_handler(array('Somm','phpExceptionHandler'));
   }
   

   
   public static function phpErrorHandler()
   {
   
   }
   
   
   
   public static function phpExceptionHandler()
   {
   
   }

  
   public static function Autoload($className)
   {
    if(!class_exists($className))
    {
     for($i=0;$i<count(self::$_namespaces);$i++)
     {
      $f = self::$_namespaces[$i].'/'.$className.self::CLASS_EXT;
      if(file_exists($f))
      {
       require_once $f;
       if(class_exists($className)) break;
      }
     }
   }
   }
   
   public static function Run($projectName = False)
   {
    $urlDecode = explode('|',$_SERVER['QUERY_STRING']);
    
    if(!$projectName)
     $projectName = $urlDecode[0];
    
    $projectPath = self::PROJECT_PATH.$projectName;
     
    if(!is_dir($projectPath))
     throw new Exception('Project does not exists!');
     
    self::$_project = $projectName;
    
    self::addNamespaces(PATH);
    self::addNamespaces(self::PROJECT_PATH.self::$_project.self::MODUL_PATH);
    
    $config = self::GetConfig('main.xml');
    
    if(!$config->mysql)
     throw new Exception('MySQL not defined!');
    else 
     self::$_modules['mysql']  = new CMySQL((string)$config->mysql->hostname,
     			       	            (string)$config->mysql->username,
                                            (string)$config->mysql->password,
					    (string)$config->mysql->database);
    
    
    if(is_object($config->xajax) && (string)$config->xajax->enabled == 'yes') {  
     self::$_modules['xajax'] = new xajax((string)$config->xajax->requesturi);
     self::Get('xajax')->configure('javascript URI',(string)$config->xajax->javascripturi);
    }
    
    if(is_object($config->smarty) && (string)$config->smarty->enabled == 'yes') {
     self::$_modules['smarty']          = new smarty();
     self::Get('smarty')->cache        = '/Cache';
     self::Get('smarty')->compile_dir  = PATH.'/Smarty/compile';
     self::Get('smarty')->template_dir = self::PROJECT_PATH.self::$_project.self::TEMPLATE_PATH;
    }
    
    
     foreach($config->modul AS $value)
     {
      self::addModul((string)$value->name,(string)$value->class,!empty($value->config)?(string)$value->config:false);
     }
     
     if(!is_object(self::$_main = new __Main()))
      throw new Exception('failed init __Main!');
    }


   public static function AddNamespaces($list)
   {
    self::$_namespaces[count(self::$_namespaces)] = $list;
    $v = GetDirTree($list,false);
    for($i=count(self::$_namespaces),$n=0;$n<=count($v)-1;$n++,$i++)
     self::$_namespaces[$i] = $v[$n];
   }


    private static function ParseConfig($conf)
    {
     $r = array();
     if(!empty($conf->var) && !empty($conf->var[0]))
     {
      foreach($conf->var AS $vc)
      {
       $r[(string)$vc->name] = (string)$vc->value;
      }
     } else {
      $r[(string)$conf->var->name] = (string)$conf->var->value;
     }
     return $r;
    }
   
   
   public static function AddModul($name, $className, $config = false)
   {
    self::$_modules[$name] = new $className();
    if(is_object(self::$_modules[$name]))
    {
     if($config != false)
     {
      $conf = self::objConfig($config);
      foreach(self::parseConfig($conf) AS $key=>$value)
      {
       self::obj($name)->{$key} = $value;
      }
     }
    } else {
     throw new Exception('Failed loading Modul!');
    }
   }
   
   
   public static function Get($modulName)
   {
    if(is_object(self::$_modules[$modulName]))
     return self::$_modules[$modulName];
    else
     throw new Exception('objed undefined modul '.$modulName);
   }
   
   
   public static function GetConfig($configFile)
   {
    $configPath = self::PROJECT_PATH.self::$_project.self::CONFIG_PATH;
    
    if(!self::$_project)
     throw new Exception('Couldn`t load Project to load config file!');
    
    if(!is_dir($configPath))
     throw new Exception('Config path does not exists!');
     
    if(!file_exists($configPath.$configFile))
     throw new Exception('Config file '.$configPath.$configFile.'does not exists!');
     
    $config = simplexml_load_file($configPath.$configFile);
    
    if(!is_object($config))
     throw new Exception('SimpleXML parse Error!');
     
    return $config;
   }
   
  }
  
  
  
  
  
  //_::initErrorHandlers();